#include "multipagesheetwidget.h"
#include "ui_multipagesheetwidget.h"

// CHARACTER MODEL
#include "toccharmodel.h"

// FILE I/O
#include <QFile>

// JSON
#include <qjson/serializer.h>
#include <qjson/qobjecthelper.h>
#include <qjson/parser.h>

// PAGE 1
#include "sheetwidget.h"

// PAGE 2
#include "sheetwidgetpg2.h"

// PAGE 3
#include "sheetwidgetpg3.h"

#include <QDebug>


MultiPageSheetWidget::MultiPageSheetWidget(QWidget *parent) :
    WidgetWithGdrData(parent),
    ui(new Ui::MultiPageSheetWidget), gdrData(0), ch(0)
{
    ui->setupUi(this);

    ch = new TocCharModel(this);

    ui->stackedWidget->addWidget(
                new SheetWidget(ch, this));

    ui->stackedWidget->addWidget(
                new SheetWidgetPg2(ch,this));

    ui->stackedWidget->addWidget(
                new SheetWidgetPg3(ch,this));

    ui->pbNext->setText("");
    ui->pbPrev->setText("");
    ui->pbNext->setIcon(
                style()->standardIcon(QStyle::SP_ArrowForward));
    ui->pbPrev->setIcon(
                style()->standardIcon(QStyle::SP_ArrowBack));

    connect(ui->pbNext, SIGNAL(clicked()), SLOT(nextPage()));
    connect(ui->pbPrev, SIGNAL(clicked()), SLOT(prevPage()));
}

MultiPageSheetWidget::~MultiPageSheetWidget()
{
    for(int i = 0; i < ui->stackedWidget->count(); ++i)
        delete ui->stackedWidget->widget(i);
    delete ui;
}

void MultiPageSheetWidget::setGdrData(QVariantMap *data)
{
    gdrData = data;

    qDebug() << "Gdr Name: " << gdrData->value("gdrname").toString();
    qDebug() << "Gdr Data Locale: " << gdrData->value("locale").toString();
    qDebug() << "Gdr Data Version: " << gdrData->value("version").toString();

    ch->setProperty("DataVersion", gdrData->value("version"));
    ch->setProperty("DataLocale", gdrData->value("locale"));

    for(int i = 0; i < ui->stackedWidget->count(); ++i)
    {
        WidgetWithGdrData* ptr = qobject_cast<WidgetWithGdrData*>(ui->stackedWidget->widget(i));
        ptr->setGdrData(data);
    }
}

QObject * MultiPageSheetWidget::chModel()
{
    return ch;
}

bool MultiPageSheetWidget::saveTo(QString file)
{
    QFile outfile(file);
    if ( !outfile.open(QFile::WriteOnly) )
        return false;

    return saveTo(&outfile);
}

bool MultiPageSheetWidget::saveTo(QIODevice* io)
{
    if ( io == 0 || !io->isOpen() )
        return false;

    for(int i = 0; i < ui->stackedWidget->count(); ++i)
    {
        WidgetWithGdrData* ptr = qobject_cast<WidgetWithGdrData*>(ui->stackedWidget->widget(i));
        ptr->updateData(ch);
    }

    QVariantMap variant = QJson::QObjectHelper::qobject2qvariant(ch);
    QJson::Serializer serializer;
    io->write(serializer.serialize(variant));
    io->close();
    return true;
}

bool MultiPageSheetWidget::loadFrom(QString file)
{
    QFile infile(file);
    if ( !infile.open(QFile::ReadOnly) )
        return false;

    return loadFrom(&infile);
}

bool MultiPageSheetWidget::loadFrom(QIODevice* io)
{
    if ( io == 0 || !io->isOpen() )
        return false;

    QJson::Parser parser;
    bool ok = false;
    QVariant variant = parser.parse(io->readAll(), &ok);
    io->close();

    if ( !ok )
        return false;

    for(int i = 0; i < ui->stackedWidget->count(); ++i)
    {
        WidgetWithGdrData* ptr = qobject_cast<WidgetWithGdrData*>(ui->stackedWidget->widget(i));
        ptr->beginLoadFrom();
    }

    QJson::QObjectHelper::qvariant2qobject(variant.toMap(), ch);

    QString chDataVersion = ch->property("DataVersion").toString();
    QString chDataLocale = ch->property("DataLocale").toString();

    if ( chDataVersion != gdrData->value("version").toString() )
    {
        emit incompatibleDataVersion(chDataVersion, gdrData->value("version").toString());
        return false;
    }

    if ( chDataLocale != gdrData->value("locale").toString() )
    {
        emit incompatibleDataLocale(chDataLocale, gdrData->value("locale").toString());
        return false;
    }

    for(int i = 0; i < ui->stackedWidget->count(); ++i)
    {
        WidgetWithGdrData* ptr = qobject_cast<WidgetWithGdrData*>(ui->stackedWidget->widget(i));
        ptr->refreshFromData(ch);
        ptr->endLoadFrom();
    }

    return true;
}

void MultiPageSheetWidget::nextPage()
{
    int cpage = ui->stackedWidget->currentIndex();
    if ( cpage < ui->stackedWidget->count() )
        ui->stackedWidget->setCurrentIndex(cpage+1);
    ui->lcdPage->display(ui->stackedWidget->currentIndex() + 1);
}

void MultiPageSheetWidget::prevPage()
{
    int cpage = ui->stackedWidget->currentIndex();
    if ( cpage > 0 )
        ui->stackedWidget->setCurrentIndex(cpage-1);
    ui->lcdPage->display(ui->stackedWidget->currentIndex() + 1);
}

void MultiPageSheetWidget::updateData(QObject* model)
{
    for(int i = 0; i < ui->stackedWidget->count(); ++i)
    {
        WidgetWithGdrData* ptr = qobject_cast<WidgetWithGdrData*>(ui->stackedWidget->widget(i));
        ptr->updateData(model);
    }
}
